require 'rubygems'
require 'win32console'
include Win32::Console::ANSI

# ANSI Escape Color List
# Intensity    0        1        2        3        4        5        6        7
# ------------------------------------------------------------------------------------
# Normal       Black    Red      Green    Yellow   Blue     Magenta  Cyan     White
# Bright       Black    Red      Green    Yellow   Blue     Magenta  Cyan     White

class String
    ANSI_DARK_RED = "\e[0m\e[31m"
    ANSI_DARK_GREEN = "\e[0m\e[32m"
    ANSI_DARK_YELLOW = "\e[0m\e[33m"
    ANSI_DARK_BLUE = "\e[0m\e[34m"
    ANSI_DARK_MAGENTA = "\e[0m\e[35m"
	ANSI_DARK_CYAN = "\e[0m\e[36m"
	ANSI_DARK_WHITE = "\e[0m\e[37m"
    ANSI_RED = "\e[1m\e[31m"
    ANSI_GREEN = "\e[1m\e[32m"
    ANSI_YELLOW = "\e[1m\e[33m"
    ANSI_BLUE = "\e[1m\e[34m"
    ANSI_MAGENTA = "\e[1m\e[35m"
	ANSI_CYAN = "\e[1m\e[36m"
	ANSI_WHITE = "\e[1m\e[37m"
	ANSI_END = "\e[0m"

	SUPPORTED_METHODS = [
		"dark_red",
		"dark_green",
		"dark_yellow",
		"dark_blue",
		"dark_magenta",
		"dark_cyan",
		"dark_white",
		"red",
		"green",
		"yellow",
		"blue",
		"magenta",
		"cyan",
		"white"
	]
	
    def dark_red; colorize(self, "#{ANSI_DARK_RED}"); end
    def dark_green; colorize(self, "#{ANSI_DARK_GREEN}"); end
    def dark_yellow; colorize(self, "#{ANSI_DARK_YELLOW}"); end
    def dark_blue; colorize(self, "#{ANSI_DARK_BLUE}"); end
    def dark_magenta; colorize(self, "#{ANSI_DARK_MAGENTA}"); end
	def dark_cyan; colorize(self, "#{ANSI_DARK_CYAN}"); end
	def dark_white; colorize(self, "#{ANSI_DARK_WHITE}"); end
    def red; colorize(self, "#{ANSI_RED}"); end
    def green; colorize(self, "#{ANSI_GREEN}"); end
    def yellow; colorize(self, "#{ANSI_YELLOW}"); end
    def blue; colorize(self, "#{ANSI_BLUE}"); end
    def magenta; colorize(self, "#{ANSI_MAGENTA}"); end
	def cyan; colorize(self, "#{ANSI_CYAN}"); end
	def white; colorize(self, "#{ANSI_WHITE}"); end
    def colorize(text, color_code)  "#{color_code}#{text}#{ANSI_END}" end
	
	def call(method)
		if SUPPORTED_METHODS.include?(method)
			self.send(method)
		else
			self
		end
	end
end

def help()
	puts "puts - output string in colors".cyan
	puts "usage: puts [--color=COLOR] message".green
	puts "  COLOR    color symbol"
	String::SUPPORTED_METHODS.each do |color|
		puts "           #{color}".call(color)
	end
	puts "example:\n  puts --color=red hello --color=green world".magenta
end

def main()
	if ARGV.size == 0
		help()
		return
	else
		color = "?"
		ARGV.each do |arg|
			if arg.start_with?("--color=")
				color = arg.split('=')[1]
			else
				puts arg.call(color)
			end
		end
	end
end

main
